package com.goethe.translator;

import com.goethe.translator.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.Service;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class Translator extends Activity {
	public final static String TAG = "Translator";
	public final static String WORDTOTRANSLATE = "com.goethe.translator.MESSAGE";
	public final static String TARGETLANGUAGE = "com.goethe.translator.TARGETLANGUAGE";
	public final static String ORIGINALLANGUAGE = "com.goethe.translator.ORIGINALLANGUAGE";
	public final static String LANGUAGE_GERMAN = "LanguageGerman";
	public final static String LANGUAGE_SPANISH = "LanguageSpanish";
	private String originalLanguage = "";
	private GUIUtils guiUtils = new GUIUtils();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_translator);
        
        Spinner spinner = (Spinner) findViewById(R.id.language_spinner);
	    // Create an ArrayAdapter using the string array and a default spinner layout
	    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.language_array, android.R.layout.simple_spinner_item);
	    // Specify the layout to use when the list of choices appears
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    // Apply the adapter to the spinner
	    spinner.setAdapter(adapter);
	    // Set the OnItemSelectedListener
	    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
	        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
	        	if(position==0){
	        		originalLanguage = LANGUAGE_SPANISH;
	        	}else if(position==1){
	        		originalLanguage = LANGUAGE_GERMAN;
	        	}
	        }

	        public void onNothingSelected(AdapterView<?> parentView) {
	            // your code here
	        }

	    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_translator, menu);
        return true;
    }
    
    // Called when the user clicks the "Traducir" button
    public void translate(View view) {
    	// We get the information contained in the edit text box
    	EditText editText = (EditText) findViewById(R.id.edit_message);
    	String key = editText.getText().toString();
    	
    	// We take the soft keyboard out of the screen
    	InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
    	imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); 
        
        // We make the query to the database
    	TranslationsOpenHelper transOpenHelper = new TranslationsOpenHelper(getApplicationContext());
    	SQLiteDatabase db = transOpenHelper.getReadableDatabase();
    	
		String table = TranslationsOpenHelper.TRANSLATOR_TABLE_NAME;
		String[] columns = null;
		String selection ;
		String[] selectionArgs = new String[]{key};
		String groupBy = null;
		String having = null;
		String orderBy = null;
		
		if(originalLanguage.equals(LANGUAGE_SPANISH)){
			selection = TranslationsOpenHelper.SPANISH_WORD + " LIKE ?";
		}else{
			selection = TranslationsOpenHelper.SPANISH_WORD + " LIKE ?";
		}
		
		Cursor queryResult = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
        
        if((queryResult == null) || !(queryResult.getCount()>0)){
        	// We print the "Word Not Found" message in the screen
        	guiUtils.showWordNotFound(this);
        }else{
	        // We move to the first result
	        queryResult.moveToFirst();
        	
	        // We print the results in the screen
        	guiUtils.showTranslatorResults(queryResult, this);
	        
        }
        
        // Closing the db object to avoid memory leaks
        db.close();
    }
}
